<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-4-30
 * Time: 下午6:33
 */
require_once LIBS_PATH . 'getui/IGt.Push.php';
require_once LIBS_PATH . 'getui/igetui/IGt.AppMessage.php';
require_once LIBS_PATH . 'getui/igetui/IGt.APNPayload.php';
require_once LIBS_PATH . 'getui/igetui/template/IGt.BaseTemplate.php';
require_once LIBS_PATH . 'getui/IGt.Batch.php';
require_once LIBS_PATH . 'getui/igetui/utils/AppConditions.php';
require_once LIBS_PATH . 'getui/getui_config.php';

class getuipush {
    private  $host;
    private  $appkey;
    private  $appid;
    private  $master;
    public function __construct() {
        $this->host = Getui_config::HOST;
        $this->appkey = Getui_config::APPKEY;
        $this->master = Getui_config::MASTERSECRET;
        $this->appid = Getui_config::APPID;

    }
    public function pushMessageToSingle($cid,$token,$msg) {
        $igt = new IGeTui($this->host, $this->appkey,$this->master,false);

        $template = $this->IGtTransmissionTemplateDemo($msg);

        //个推信息体
        $message = new IGtSingleMessage();

        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(3600*12*1000);//离线时间
        $message->set_data($template);//设置推送消息类型
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        //接收方
        $target = new IGtTarget();
        $target->set_appId($this->appid);
        $target->set_clientId($cid);
        try {
            $rep = $igt->pushMessageToSingle($message, $target);


        }catch(RequestException $e){
            $requstId =e.getRequestId();
            $rep = $igt->pushMessageToSingle($message, $target,$requstId);
        }
        //return $rep;

    }
    function IGtTransmissionTemplateDemo($msg){
        $template =  new IGtTransmissionTemplate();
        $template->set_appId($this->appid);//应用appid
        $template->set_appkey($this->appkey);//应用appkey
        $template->set_transmissionType(2);//透传消息类型
        $template->set_transmissionContent(json_encode($msg));//透传内容


        $apn = new IGtAPNPayload();
        $alertmsg=new SimpleAlertMsg();
        $alertmsg->alertMsg=$msg['cap'];
        $apn->alertMsg=$alertmsg;
        $apn->badge=2;
        $apn->sound="";
        $apn->add_customMsg("payload",json_encode($msg));
        //$apn->contentAvailable=1;
        $apn->category="ACTIONABLE";
        $template->set_apnInfo($apn);


        /*$apn = new IGtAPNPayload();
        $alertmsg=new DictionaryAlertMsg();
        $alertmsg->body=$msg['cap'];
        $alertmsg->actionLocKey="ActionLockey";
        $alertmsg->locKey="LocKey";
        $alertmsg->locArgs=array("locargs");
        $alertmsg->launchImage="launchimage";
        //iOS8.2 支持
        $alertmsg->title=$msg['cap'];
        $alertmsg->titleLocKey="TitleLocKey";
        $alertmsg->titleLocArgs=array("TitleLocArg");

        $apn->alertMsg=$alertmsg;
        $apn->badge=1;
        $apn->sound="test1.wav";
        $apn->add_customMsg("payload",json_encode($msg));
        $apn->contentAvailable=1;
        $apn->category="ACTIONABLE";
        $template->set_apnInfo($apn);*/

        return $template;
    }

}